package com.framework.websocket.example.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 传感器数据控制器 - 演示新订阅类型的使用
 */
@RestController
@RequestMapping("/api/sensor")
public class SensorController {
    
    @Autowired
    private RedisTemplate<String, Object> redisTemplate;
    
    /**
     * 更新传感器数据
     * 
     * @param sensorId 传感器ID
     * @param type 传感器类型（temperature/humidity/pressure等）
     * @param value 传感器值
     */
    @PostMapping("/{sensorId}/data")
    public Map<String, Object> updateSensorData(
            @PathVariable String sensorId,
            @RequestParam String type,
            @RequestParam Double value) {
        
        Map<String, Object> sensorData = new HashMap<>();
        sensorData.put("sensorId", sensorId);
        sensorData.put("type", type);
        sensorData.put("value", value);
        sensorData.put("unit", getUnit(type));
        sensorData.put("timestamp", LocalDateTime.now().toString());
        
        // 保存到Redis，自动触发推送
        String key = "sensor:data:" + sensorId;
        redisTemplate.opsForValue().set(key, sensorData);
        
        Map<String, Object> result = new HashMap<>();
        result.put("success", true);
        result.put("message", "传感器数据已更新");
        result.put("data", sensorData);
        return result;
    }
    
    /**
     * 批量更新传感器数据
     */
    @PostMapping("/batch")
    public Map<String, Object> batchUpdateSensorData(@RequestBody List<SensorDataRequest> requests) {
        Map<String, Object> results = new HashMap<>();
        
        for (SensorDataRequest request : requests) {
            Map<String, Object> sensorData = new HashMap<>();
            sensorData.put("sensorId", request.getSensorId());
            sensorData.put("type", request.getType());
            sensorData.put("value", request.getValue());
            sensorData.put("unit", getUnit(request.getType()));
            sensorData.put("timestamp", LocalDateTime.now().toString());
            
            String key = "sensor:data:" + request.getSensorId();
            redisTemplate.opsForValue().set(key, sensorData);
            
            results.put(request.getSensorId(), sensorData);
        }
        
        Map<String, Object> response = new HashMap<>();
        response.put("success", true);
        response.put("message", "批量更新成功");
        response.put("count", requests.size());
        response.put("data", results);
        return response;
    }
    
    private String getUnit(String type) {
        switch (type.toLowerCase()) {
            case "temperature":
                return "°C";
            case "humidity":
                return "%";
            case "pressure":
                return "hPa";
            case "light":
                return "lux";
            case "co2":
                return "ppm";
            default:
                return "";
        }
    }
    
    /**
     * 传感器数据请求对象
     */
    public static class SensorDataRequest {
        private String sensorId;
        private String type;
        private Double value;
        
        // Getters and setters
        public String getSensorId() { return sensorId; }
        public void setSensorId(String sensorId) { this.sensorId = sensorId; }
        public String getType() { return type; }
        public void setType(String type) { this.type = type; }
        public Double getValue() { return value; }
        public void setValue(Double value) { this.value = value; }
    }
}